package look4ss.aplicativo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;

public class posLogin extends AppCompatActivity {
    public SharedPreferences sharp;
    public SharedPreferences.Editor sharp_edt;
    public static String destino;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coisas);
        sharp = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        obterUID();
        obterDataRegistro();
        obterIP();
        obterURLFotoPerfil();
        obterNome();
        continuar();
    }

    private void continuar(){
        startActivity(new Intent(posLogin.this, ActivityEnviar.class));
    }


    public void obterUID() {
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiObterUID, new Response.Listener<String>() {
            Map<String, String> params = new HashMap<>();

            @Override
            public void onResponse(String resposta) {
                sharp_edt = sharp.edit();
                sharp_edt.putString("uid", resposta);
                sharp_edt.commit();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("email", sharp.getString("email", ""));
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }

    public void obterNome() {
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiObterDadosConta, new Response.Listener<String>() {
            Map<String, String> params = new HashMap<>();

            @Override
            public void onResponse(String resposta) {
                sharp_edt = sharp.edit();
                sharp_edt.putString("nome", resposta);
                sharp_edt.commit();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("uid", sharp.getString("uid", ""));
                params.put("dado", "nome");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }

    public void obterDataRegistro() {
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiObterDadosConta, new Response.Listener<String>() {
            Map<String, String> params = new HashMap<>();

            @Override
            public void onResponse(String resposta) {
                sharp_edt = sharp.edit();
                sharp_edt.putString("data_registro", resposta);
                sharp_edt.commit();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("uid", sharp.getString("uid", ""));
                params.put("dado", "tempo_registro");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }

    public void obterIP() {
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiObterDadosConta, new Response.Listener<String>() {
            Map<String, String> params = new HashMap<>();

            @Override
            public void onResponse(String resposta) {
                sharp_edt = sharp.edit();
                sharp_edt.putString("IP_registro", resposta);
                sharp_edt.commit();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("uid", sharp.getString("uid", ""));
                params.put("dado", "IP");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }

    public void obterURLFotoPerfil() {
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiObterDadosConta, new Response.Listener<String>() {
            Map<String, String> params = new HashMap<>();

            @Override
            public void onResponse(String resposta) {
                sharp_edt = sharp.edit();
                sharp_edt.putString("nome", resposta);
                sharp_edt.commit();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("uid", sharp.getString("uid", ""));
                params.put("dado", "URLFotoPerfil");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }
}